package cn.zsb.proxy.jdkProxy;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class ProxyFactory implements InvocationHandler{

    //目标对象(被代理对象)
    private Object target;

    public ProxyFactory() {

    }

    public ProxyFactory(Object target) {
        this.target = target;
    }

    public void setTarget(Object target) {
        this.target = target;
    }

    public Object getProxyInstance() {
        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(), //目标对象类加载器
                target.getClass().getInterfaces(), //目标对象的接口类型
               this
                );
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("jdk动态代理开始");
        System.out.println(proxy.getClass()+">>>>>");
        System.out.println(proxy.getClass().getSuperclass()+"父类");
        Class<?>[] interfaces = proxy.getClass().getInterfaces();
        System.out.println("所有的接口："+ Arrays.toString(interfaces));
        Field[] fr = proxy.getClass().getDeclaredFields();
        System.out.println("所有的属性：");
        for(Field f:fr){
            System.out.println(f);
        }
        Object returnVal = method.invoke(target, args);
        System.out.println("jdk动态代理结束");
        return returnVal;//目标对象方法返回值
    }
}
